package com.kinon.bs_app.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.bs_app.R;
import com.kinon.bs_app.activities.MessageActivity;
import com.kinon.bs_app.adapter.MsgAdapter;
import com.kinon.bs_app.beans.MsgBean;
import com.kinon.bs_app.beans.MsgDataBean;
import com.kinon.bs_app.utils.HttpUtils;
import com.kinon.bs_app.utils.PrefUtils;
import com.kinon.bs_app.utils.http.StrCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class UpMsgFragment extends Fragment {

    private View mView = null;

    private RecyclerView rv_list = null;
    private SmartRefreshLayout layout = null;

    private List<MsgDataBean> list = new ArrayList<>();
    private MsgAdapter adapter = null;

    private int page = 10;
    private int offset = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_upmsg, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        getData();
    }

    private void initView() {
        rv_list = mView.findViewById(R.id.rv_list);
        layout = mView.findViewById(R.id.sr_refresh_layout);
        setSmartRefreshLayout();

        LinearLayoutManager layoutManager = new LinearLayoutManager(mView.getContext());
        rv_list.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(mView.getContext(), list);
        rv_list.setAdapter(adapter);
    }

    private void setSmartRefreshLayout() {
        /**
         * 刷新
         */
        layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2* 1000);
                list.clear();
                offset = 0;
                getData();
            }
        });
        /**
         * 加载更多
         */
        layout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2* 1000);
                offset += page;
                getData();
            }
        });
    }

    private void getData() {
        Call<String> call = HttpUtils.getRequestApi().upTip(PrefUtils.getToken(), page, offset);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
//                Log.d("zzy", "up-data: "+data);
                Gson gson = new Gson();
                MsgBean msgBean = gson.fromJson(data, MsgBean.class);
                list.addAll(msgBean.getDatas());
                Log.d("zzy", "up-data: "+list.toString());
                adapter.update(list);

            }

            @Override
            public void onError(int code, String error) {
                Log.d("zzy", "error: "+error);
            }
        });
    }
}
